﻿#include "DeviceManageWidget.h"
#include "ui_DeviceManageWidget.h"
#include "CommonFunc.h"
#include "DataInfo.h"

DeviceManageWidget::DeviceManageWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DeviceManageWidget)
{
    ui->setupUi(this);

    appendTypeToFunc(XShowDevice, std::bind(&DeviceManageWidget::updateDeviceInfo, this));

}

DeviceManageWidget::~DeviceManageWidget()
{
    delete ui;
}

void DeviceManageWidget::updateDeviceInfo()
{
    if (g_iProductLineID == -1 || g_iGateWayID == -1)
    {
        clearDeviceData();
        return;
    }

    for (int i = 0; i < g_vecProductInfo.size(); ++i)
    {
        const ProductInfo& pi = g_vecProductInfo[i];
        if (pi.id == g_iProductLineID)
        {
            for (int j = 0; j < g_vecGateWay.size(); ++j)
            {
                const GateWay& gw = g_vecGateWay[j];
                if (gw.id == g_iGateWayID)
                {
                    int iStop{0}, iSlow{0};
                    QMapIterator<int, QList<Sensor> > iter(g_mapGroupIdToListSensor);
                    while (iter.hasNext()) {
                        iter.next();
//                        int gID = iter.key();
                        for (int i = 0; i < iter.value().size(); ++i)
                        {
                            const Sensor& ss = iter.value()[i];
                            if (ss.status == "stop")
                            {
                                ++iStop;
                            }
                            else if (ss.status == "slow")
                            {
                                ++iSlow;
                            }
                        }
                    }
                    QString title = QString("%1-%2  %3%4  %5%6").arg(pi.strName).arg(gw.strName).arg(QStringLiteral("停转"))
                            .arg(iStop).arg(QStringLiteral("低速")).arg(iSlow);
                    ui->toolButton_title->setText(title);
                    g_strGateStateInfo = title;
                    break;
                }
            }
            break;
        }
    }

    ui->deviceWidget->clearAndShowDeviceInfo();
}

void DeviceManageWidget::clearDeviceData()
{
    g_mapGroupIdToListSensor.clear();
    ui->deviceWidget->clearAndShowDeviceInfo();
    g_strGateStateInfo.clear();
}

void DeviceManageWidget::on_pushButton_returnUp_clicked()
{
    executeFuncFromID(XUI_GateWayManage);
}

void DeviceManageWidget::on_pushButton_returnTop_clicked()
{
    executeFuncFromID(XUI_Login);
}

void DeviceManageWidget::on_pushButton_warn_clicked()
{
    executeFuncFromID(XUI_AbnormalManage);
}

void DeviceManageWidget::on_pushButton_speed_clicked()
{
    executeFuncFromID(XUI_ShowSpeed);
}
